package com.tubang.logistics.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tubang.common.constant.Constants;
import com.tubang.logistics.entity.PlatformOrderBiddingEntity;
import com.tubang.logistics.mapper.PlatformOrderBiddingMapper;
import com.tubang.logistics.service.IPlatformOrderBiddingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 平台竞价表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2018-01-15
 */
@Service
public class PlatformOrderBiddingServiceImpl extends ServiceImpl<PlatformOrderBiddingMapper, PlatformOrderBiddingEntity> implements IPlatformOrderBiddingService {

    @Override
    public int selectByCarrierIdAndOrderId(String orderId, String companyId) {
        EntityWrapper<PlatformOrderBiddingEntity> logisticsWrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(companyId)) {
            logisticsWrapper.in("carrier_id", companyId);
        }
        if (StringUtils.isNotEmpty(orderId)) {
            logisticsWrapper.like("order_id", orderId);
        }
        int logisticsCount = selectCount(logisticsWrapper);
        return logisticsCount;
    }

    @Override
    public PlatformOrderBiddingEntity selectPloByCarrierIdAndOrderId(String orderId, String companyId) {
        EntityWrapper<PlatformOrderBiddingEntity> logisticsWrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(companyId)) {
            logisticsWrapper.in("carrier_id", companyId);
        }
        if (StringUtils.isNotEmpty(orderId)) {
            logisticsWrapper.like("order_id", orderId);
        }
        logisticsWrapper.eq("enable", Constants.DEFAULT_VAULE_ONE);
        PlatformOrderBiddingEntity platformOrderBiddingEntity = selectOne(logisticsWrapper);
        return platformOrderBiddingEntity;
    }

    @Override
    public void deleteByCarrierIdAndOrderId(String carrierId, String orderId) {
        EntityWrapper<PlatformOrderBiddingEntity> logisticsWrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(carrierId)) {
            logisticsWrapper.in("carrier_id", carrierId);
        }
        if (StringUtils.isNotEmpty(orderId)) {
            logisticsWrapper.like("order_id", orderId);
        }
        List<PlatformOrderBiddingEntity> platformOrderBiddingEntities = selectList(logisticsWrapper);
        for (PlatformOrderBiddingEntity platformOrderBiddingEntity : platformOrderBiddingEntities) {
            platformOrderBiddingEntity.setEnable(false);
            updateById(platformOrderBiddingEntity);
        }
    }
}
